package com.dbq.fast.business.me.api;

import com.dbq.fast.business.login.api.LoginApi;
import com.dbq.fast.business.me.bean.ActiveMoney;
import com.dbq.fast.business.me.bean.RewardInfo;
import com.dbq.fast.business.me.bean.WithdrawInfo;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.HashMap;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class RewardApi {

    private static HttpManager httpManager = HttpManager.getInstance();

    public static void queryActiveMoney(RemoteApiCallBack<ActiveMoney> c){
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        httpManager.getHttpService()
                .queryActiveMoney(token, vpnId)
                .compose(new CommonTransformer<>())
                .subscribe(activeMoneyHttpResult -> {
                    if(activeMoneyHttpResult.isSuccess()){
                        c.onSuccess(activeMoneyHttpResult.getData());
                    }else if(activeMoneyHttpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            queryActiveMoney(c);
                        }).start();

                    }else {

                    }
                }, throwable -> {

                });
    }

    public static void queryMyReward(int page, RemoteApiCallBack<RewardInfo> c){
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        HashMap body = new HashMap();
        body.put("vpnId", vpnId);
        body.put("page", page);
        body.put("size", 10);
        httpManager.getHttpService()
                .queryRewardInfo(token, body)
                .compose(new CommonTransformer<>())
                .subscribe(rewardInfoHttpResult -> {
                    if(rewardInfoHttpResult.isSuccess()){
                        c.onSuccess(rewardInfoHttpResult.getData());
                    }else if(rewardInfoHttpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            queryMyReward(page, c);
                        }).start();

                    }else {

                    }
                }, throwable -> {

                });
    }

    public static void queryWithdrawRecord(int page, RemoteApiCallBack<WithdrawInfo> c){
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        HashMap body = new HashMap();
        body.put("vpnId", vpnId);
        body.put("page", page);
        body.put("size", 10);
        httpManager.getHttpService()
                .queryWithdrawInfo(token, body)
                .compose(new CommonTransformer<>())
                .subscribe(listHttpResult -> {
                    if(listHttpResult.isSuccess()){
                        c.onSuccess(listHttpResult.getData());
                    }else if(listHttpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            queryWithdrawRecord(page, c);
                        }).start();

                    }else {

                    }
                }, throwable -> {

                });
    }

    public static void bindWx(String wxAccount, RemoteApiCallBack c){
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        HashMap body = new HashMap();
        body.put("vpnId", vpnId);
        body.put("wechat", wxAccount);
        httpManager.getHttpService()
                .bindWx(token, body)
                .compose(new CommonTransformer<>())
                .subscribe(stringHttpResult -> {
                    if (stringHttpResult.isSuccess()) {
                        c.onSuccess("");
                    }else if(stringHttpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            bindWx(wxAccount, c);
                        }).start();

                    }else {
                        c.onFail(stringHttpResult.getCode(), stringHttpResult.getMsg());
                    }
                }, throwable -> {
                    c.onFail(-1, "");
                });
    }

    public static void withdraw(int balance, RemoteApiCallBack c){
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        HashMap body = new HashMap();
        body.put("vpnId", vpnId);
        body.put("balance", balance);
        httpManager.getHttpService()
                .withdraw(token, body)
                .compose(new CommonTransformer<>())
                .subscribe(result -> {
                    if(result.isSuccess()){
                        c.onSuccess("");
                    }else if(result.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            withdraw(balance, c);
                        }).start();

                    }else {
                        c.onFail(result.getCode(), result.getMsg());
                    }
                }, throwable -> {
                    c.onFail(-1, "");
                });
    }

}
